import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
//import java.util.Calendar;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.DataLine.Info;

public class MakeAudioFile {
	
	
	FileInputStream waveStream;
	AudioInputStream audioInputStream = null;
	AudioFormat audioFormat;
	Info info;
	SourceDataLine dataLine = null;
	long size=90000000;
	FileWriter outFile;
	PrintWriter out;
	int frameSize=919;
	long frameData[]=new long[1444*918];
	long prev=0;
	int frameCtr=0;
	int Counter=0;
	int length=0;
	int inc=0;
	byte[] dataAudio=new byte[9000000];
	
	//final int buffer_size_bytes = 10*1024; 
	int buffer_size_bytes;// = 10*1024; 
	
	static long last_time = 0;
	
	public void AudioFile(String in_wav_file, int vid_fps) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
    	
		File fileOut = new File("finalSound.wav");
		
		Global.Shots[0][0]=300;
    	Global.Shots[0][1]=352;
    	Global.Shots[1][0]=1000;
    	Global.Shots[1][1]=1020;
		
		try {
    		waveStream = new FileInputStream(in_wav_file);
    	    } catch (FileNotFoundException e) {
    	    e.printStackTrace();
    	    return;
    	   }
    	
   	    
    	
    	audioInputStream = AudioSystem.getAudioInputStream(this.waveStream);

    	// Obtain the information about the AudioInputStream
    	audioFormat = audioInputStream.getFormat();
    	info = new Info(SourceDataLine.class, audioFormat);
    	
    	// opens the audio channel
	    dataLine = (SourceDataLine) AudioSystem.getLine(info);
	    dataLine.open(audioFormat, (buffer_size_bytes));
	    
	    //Prepare output for writing
	    outFile = new FileWriter("dump.txt");
	    out = new PrintWriter(outFile);

    	// Starts the music :P
    	dataLine.start();
    	int shotsCtr=0;
    	
    	
    	while(shotsCtr<Global.numShots)
    	
    	{
    	int startByte=(Global.Shots[shotsCtr][0]/4)*3675*2;
    	int stopByte=(Global.Shots[shotsCtr][1]/4)*3675*2;
    	
    	int bytesToRead=(Global.Shots[shotsCtr][1]-Global.Shots[shotsCtr][0])*3675;
    	
    	System.out.println("bytes to read: " + bytesToRead+"\t startByte"+startByte);
    	buffer_size_bytes=bytesToRead;
    	length=buffer_size_bytes;
    	try {
    		System.out.println("1111 "+audioInputStream.skip(startByte));
    		}
    		catch (Exception e) {
    		System.out.println(e);
    		}
	
	    int readBytes = 0;
		byte[] audioBuffer = new byte[buffer_size_bytes];
		int counter=0;
		int dataCounter=0;
		
		System.out.println(buffer_size_bytes);
		
		do
		{


			try {

			readBytes = audioInputStream.read(audioBuffer, 0,buffer_size_bytes);
			
	
			Counter++;
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			while(Global.pause==1)
			{
				Global.cur_aud_time=Global.paused_aud_time;
				dataLine.stop();
			}
			
			dataLine.start();
			//AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, fileOut);
			
			if (readBytes >= 0){
			    
				long data = 0;
				for(int i = 0; i < readBytes; i+= 1)
				{
					 dataAudio[inc] =audioBuffer[i];
					
					// System.out.println(dataAudio[inc]);
					 inc++;
				}
				

				dataLine.write(audioBuffer, 0, readBytes);
				System.out.println(audioBuffer);
				
				
				  
				}
				
				
			
			
			
			
		} while (readBytes >= 0 && Counter<1);

        shotsCtr++;
    	}
}}
